图表库MPAndroidChart简介

介绍

A simple charting library for Android, supporting line-, bar-, scatter-, candlestick- and piecharts, as well as scaling, dragging, selecting and animations. Supporting Android 2.2 (API level 8) and upwards.

这个图表非常强大,很多属性设置起来非常方便,并且动画效果非常酷,之前用的achartengine,跟这个对比,简直low爆了,而且用起来也非常繁琐,建议大家尽早替换吧!下面以创建曲线为例作简单的讲解,其他曲线类似

github地址:https://github.com/PhilJay/MPAndroidChart

使用

  • xml

    <com.github.mikephil.charting.charts.LineChart
        android:id="@+id/chart"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    
    LineChart chart = (LineChart) findViewById(R.id.chart);
    
  • code

    LineChart chart = new LineChart(Context);
    
  • init data

    ArrayList<String> xVals = new ArrayList<String>();
    ArrayList<Entry> yVals = new ArrayList<Entry>();
    
    LineDataSet set1 = new LineDataSet(yVals, title + "曲线");
    set1.setColor(Color.RED);
    set1.setCircleColor(Color.RED);
    set1.setLineWidth(2f);
    set1.setCircleSize(4f);
    set1.setDrawCircles(true);
    set1....//可以根据需求设置一系列参数,非常详细
    
    ArrayList<LineDataSet> dataSets = new ArrayList<LineDataSet>();
    dataSets.add(set1); // add the datasets 创建多个表示多根曲线
    // create a data object with the datasets
    LineData data = new LineData(xVals, dataSets);
    //最后
    chart.setData(data);
    

几点说明

  1. 为了兼容低版本动画效果,此库必须导入第三方动画库nineoldandroids.jar,如果你是以jar包的形式应用并且你的项目已经引入了nineoldandroids.jar,请将项目中的nineoldandroids.jar删除,否则项目会报错
  2. 可以直接调用方法将图表的截图保存到图库或者sd卡,非常方便

    saveToGallery(String title): Saves the current chart state as an image to the gallery.
    saveToPath(String title, String pathOnSD): Saves the current chart state as an image to the specified path.
    
  3. 可以设置曲线展示动画方式()

    animateX(int durationMillis): Animates the charts values on the horizontal axis, meaning that the chart will build up within the specified time from left to right.
    animateY(int durationMillis): Animates the charts values on the vertical axis, meaning that the chart will build up within the specified time from bottom to top.
    animateXY(int xDuration, int yDuration): Animates both horizontal and vertical axis, resulting in a left/right bottom/top build-up.
    
  4. 可以改变图表中图例的颜色及显示位置

    Legend legend = lineChart.getLegend();
    legend.setTextColor(Color.WHITE);
    legend.setPosition(LegendPosition.BELOW_CHART_CENTER);
    
  5. 可以设置图表、x轴、y轴、图例的字体

    Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/OpenSans-SemiboldItalic.ttf");
    //图表
    chart.setValueTypeface(tf);
    //x轴
    XLabels x = chart.getXLabels();
    x.setTypeface(tf);
    //y轴
    YLabels y = lineChart.getYLabels();
    y.setTypeface(tf);
    //图例
    legend.setTypeface(tf)
    
  6. 可以给图表添加 Limit Lines

    LimitLine maxLine = new LimitLine(maxValue);
    maxLine.setLineWidth(4f);
    maxLine.enableDashedLine(10f, 10f, 0f);
    maxLine.setDrawValue(false);
    maxLine.setLabelPosition(LimitLabelPosition.RIGHT);
    
    LimitLine minLine = new LimitLine(minValue);
    minLine.setLineWidth(4f);
    minLine.enableDashedLine(10f, 10f, 0f);
    minLine.setDrawValue(false);
    minLine.setLabelPosition(LimitLabelPosition.RIGHT);
    
    lineData.addLimitLine(maxLine);
    lineData.addLimitLine(minLine);